package org.ehe.erp.controller.quality;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.business.monitor.MonitorMethod;
import org.ehe.business.overdue.extract.BomDataExtractor;
import org.ehe.business.overdue.extract.FeedBackDataExtractor;
import org.ehe.business.timeout.contants.BizTypeConstant;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.erp.domain.quality.ErpQualityFeedback;
import org.ehe.erp.domain.quality.bo.ErpQualityFeedbackBo;
import org.ehe.erp.domain.quality.dto.QualityFeedbackQueryDto;
import org.ehe.erp.domain.quality.vo.ErpQualityFeedbackVo;
import org.ehe.erp.service.quality.ErpQualityFeedbackService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 质检异常反馈控制器
 *
 * @author zhangnn
 * @date 2025-09-11
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/quality/feedback")
public class ErpQualityFeedbackController extends BaseController {

    private final ErpQualityFeedbackService feedbackService;

    /**
     * 分页列表
     */
    @SaCheckPermission("quality:feedback:list")
    @GetMapping("/list")
    public TableDataInfo<ErpQualityFeedbackVo> list(QualityFeedbackQueryDto dto, PageQuery pageQuery) {
        return feedbackService.queryPageList(dto, pageQuery);
    }

    /**
     * 不分页列表
     */
    @SaCheckPermission("quality:feedback:list")
    @GetMapping("/listAll")
    public R<List<ErpQualityFeedbackVo>> listAll(QualityFeedbackQueryDto dto) {
        return R.ok(feedbackService.queryList(dto));
    }

    /**
     * 详情
     */
    @SaCheckPermission("quality:feedback:query")
    @GetMapping("/{id}")
    public R<ErpQualityFeedbackVo> getInfo(@PathVariable Long id) {
        return R.ok(feedbackService.queryById(id));
    }

    /**
     * 新增
     */
    @SaCheckPermission("quality:feedback:add")
    @Log(title = "新增质检异常", linkType = "quality", businessType = BusinessType.INSERT, nameField = "feedbackNo")
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpQualityFeedbackBo bo) {
        return toAjax(feedbackService.insertByBo(bo));
    }

    /**
     * 修改
     */
    @SaCheckPermission("quality:feedback:edit")
    @Log(title = "修改质检异常", linkType = "quality", businessType = BusinessType.UPDATE, nameField = "feedbackNo")
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpQualityFeedbackBo bo) {
        return toAjax(feedbackService.updateByBo(bo));
    }

    /**
     * 删除
     */
    @SaCheckPermission("quality:feedback:remove")
    @Log(title = "质检异常信息删除", linkType = "quality", businessType = BusinessType.DELETE, nameField = "feedbackNo")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(feedbackService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 质检任务指派到具体的人员
     */
    @MonitorMethod(bizType = BizTypeConstant.FEEDBACK+"Overdue", extractor = FeedBackDataExtractor.class)
    @Log(title = "质检异常信息指派", linkType = "quality", businessType = BusinessType.UPDATE, nameField = "feedbackNo")
    @PostMapping("/{id}/{handlerId}")
    public R<ErpQualityFeedback> submit(@PathVariable Long id,@PathVariable Long handlerId) {
        return R.ok("质检异常信息指派成功",feedbackService.submit(id,handlerId));
    }

    /**
     * 处理/关闭反馈（状态流转）
     */
    @SaCheckPermission("quality:feedback:handle")
    @MonitorMethod(bizType = BizTypeConstant.FEEDBACK+"Overdue", extractor = FeedBackDataExtractor.class)
    @Operation(summary = "处理/关闭异常反馈")
    @Log(title = "质检异常信息处理", linkType = "quality", businessType = BusinessType.UPDATE, nameField = "feedbackNo")
    @PostMapping("/handle")
    public R<ErpQualityFeedback> handle(@RequestBody ErpQualityFeedbackBo bo) {
        Long id = bo.getId();
        Integer status = bo.getStatus();
        String handleResult = bo.getHandleResult();
        return R.ok("质检异常信息处理成功",feedbackService.handleFeedback(id, status, handleResult));
    }

    /**
     * 校验反馈单号唯一性
     */
    @Operation(summary = "校验反馈单号是否唯一")
    @GetMapping("/checkFeedbackNoUnique")
    public R<Boolean> checkFeedbackNoUnique(@RequestParam String feedbackNo,
                                            @RequestParam(required = false) Long id) {
        return R.ok(feedbackService.checkFeedbackNoUnique(feedbackNo, id));
    }
}
